<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
	public Map getMap(List<Map> result , Map<String,String> deptMap){
		int sumCount = 0;
		//处理结合数据
		for(Map map : result){
		
			String count = map.get("cont") == null ? "0" : map.get("cont").toString();
		
			String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
			map.put("name",deptMap.get(ksdm));
			
			sumCount = sumCount + Integer.parseInt(count);
		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
		resMap.put("sumCount",sumCount+"");
		return resMap;
	}
%>
<%
	//统计危重病人
	String yydm =  input.getDesc("yydm","01");//医院代码
	String date = input.getDesc("start_date","2017-12-20");//开始时间
	String endDa = input.getDesc("end_date","2017-12-25");//结束时间
	String ksdm =  input.getDesc("ksdm","-1");//科室代码
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	
	
	String sqlWhere = "";
	if(!ksdm.equals("-1")){
		sqlWhere += " and a.ksdm in("+ksdm + ")";
		
	}
	if(!yydm.equals("-1"))
	sqlWhere += " and a.ksdm in (select id from YY_KSBMK where yydm ='"+yydm+"') " ;
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name ) from his_dept ");
	Map<String,String> deptMap = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap.put(map.get("id").toString(),map.get("name").toString());
	}
	
	
	//查询危重病人
	//String hql = "select new map( a.ksdm as ksdm , count(a.ksdm) as cont) from ZY_BRSYK a , BQ_CQYZK b where a.syxh = b.syxh and ( b.yzlb = 16 or b.yzlb = 17 ) and b.ksrq >= '" + startDate + "' and b.ksrq <= '" + endDate + "' and b.yzzt in(0,1,2) group by a.ksdm order by count(a.ksdm)  desc";
	//List<Map> result = QBuilder.db("basedb").queryHql(hql);
String sql = "select  a.ksdm as ksdm , count(a.ksdm) as cont from ZY_BRSYK a left join ( select DISTINCT syxh ,bqdm, yzlb  ,yzzt from  VW_CQYZK ) b on a.syxh = b.syxh where  ( b.yzlb = 16 or b.yzlb = 17 ) and ((cyrq is null or  cqrq > '"+startDate+"')  and (rqrq < '"+endDate+"' ) )  and  brzt != 9    and b.yzzt in(0,1,2) "+sqlWhere+" group by a.ksdm order by count(a.ksdm)  desc";

	List<Map> result = new ArrayList<Map>();
	StatelessSession ss =  DBTools.db("basedb").getSf().openStatelessSession();
      // System.out.println(sql);
	try {
		 ss.beginTransaction();
		 SQLQuery sqlQuery = ss.createSQLQuery(sql);
		 sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        result = sqlQuery.list();
		  ss.getTransaction().commit();
		 ss.close();
	
    }catch (Exception e){
	   
    }
	
	
	
	Map<String,Object> resMap = new HashMap<String,Object>();
	resMap.put("ksMap",getMap(result , deptMap));
	
	
	Message msg = new Message();

	msg.put("critical",resMap);
	out.println(msg);
%>